Ext.define('ECAT.controller.ItemizeController', {
    extend: 'Ext.app.Controller',
	requires: ['ECAT.view.Itemize'],
    config: {
    	refs:{
    		topBar : 'topbar',
    		buttomBar : 'buttombar',
    		itemize : 'itemize',
    		itemizeList : 'list_itemize',
    		imageList : 'list_img_itemize'
    	},
    	control: {
            '#btn_itemize' : {
            	tap : 'tapItemizeMenu'
            },
            itemizeList : {
            	show : 'resumeImgList',
            	itemtap : 'tapItemzie'
            },
            '#list_img_itemize img': {
               tap: 'tapImg'
            }
        }
    },
    tapItemizeMenu : function(btn, e, eOpts){
    	var itemize = this.getItemize(),
    		itemizeList;
    	if(Ext.Viewport.getActiveItem() !== itemize){
	    	if(!itemize){
	    		itemize = Ext.create('ECAT.view.Itemize',{id : 'itemize'});
	    	}
	    	Ext.Viewport.setActiveItem(itemize);
	    	itemizeList = this.getItemizeList();
	    	if(!itemizeList.isHidden()){
	    		itemizeList.hide();
	    	}
			this.getTopBar().toItemizeList();
    		this.getButtomBar().setBtn(btn.id);
    		Ext.defer(function(itemizeList){
	    		itemizeList.show({  
	    			type: 'slide',  
	    			direction: 'up'  
	    		});
    		},20,this,[itemizeList]);
    	}
    },
    tapItemzie: function(itemizeList,index,target,record,e,eOpts) {
    	this.getButtomBar().btn_menu.enable();
    	this.getTopBar().toImgListItemize(record.get('name'));
    	this.getImageList().filterByItemize(record.get('id'));
        this.getItemizeList().hide({type: 'slide',direction: 'right',out: true,duration : 500});
    },
    resumeImgList : function(list, eOpts){
    	this.getImageList().getScrollable().getScroller().scrollTo(0,0);
    },
   	tapImg: function(img) {
     	var list = this.getListImgItemize(),
     		topbar = this.getTopBar(),
     		record = img.config.record,
     		type = record.get('type'),
     		itemizes = Ext.getCmp('list_itemize').getStore(),
     		model = itemizes.findRecord('id', type),
     		name = model.get('name');
    	ECAT.lib.showImageDetail(img, list.getParent(), list, name, topbar.toImgListItemize, [name]);
    }
});
